#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-06-30 23:17
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：

import os
from ctypes import windll

import win32con

import mysql_client
import logging
import pyodbc

from access_error import error_code


class Connect(mysql_client.Connect):
    """docstring for Connect"""

    def __init__(self, config):
        super(Connect, self).__init__(config)
        self.log = logging.getLogger('main.access_client')
        self.config = config
        # 连接池对象
        self.__pool = None
        self.error_code = error_code

    # 获取连接池连接
    def __get_pool_connect(self):
        return self.__get_connect()

    # 获取数据库连接
    def __get_connect(self):
        try:
            file_path = self.get_file_path()
            if not os.path.exists(file_path):
                info = "库文件：{}，不存在不能连接数据库".format(file_path).encode('gbk')
                windll.user32.MessageBoxA(0, info, "警告".encode('gbk'), 1 | win32con.MB_ICONWARNING)
                return

            ext = os.path.splitext(file_path)[1].lower()
            if ext == '.mdb':
                driver_str = 'Microsoft Access Driver (*.mdb)'
            else:
                driver_str = 'Microsoft Access Driver (*.mdb, *.accdb)'
            connect_str = "DRIVER={%s};DBQ=%s;Pwd=%s;ExtendedAnsiSQL=1;" % (
                    driver_str, file_path, self.config.get('password', '')
                    )
            return pyodbc.connect(connect_str, timeout = 1, autocommit = True, )
        except Exception as e:
            self.get_error_info(e)


if __name__ == '__main__':
    import time

    config = {
            'username': 'admin',
            # 'mode': '短连接',
            'mode': 'ODBC',
            'type': 'mysql',
            'name': 'ics',
            'host': '127.0.0.1',
            'dsn': 'CL_ASM570',
            'port': 3306,
            'password': 'ygct.3398088',
            'database': 'ygct',
            'databasepath': r'D:\CODE\PYTHON\DA_V4\app\KY8SA.mdb',
            # 'charset': 'gbk'
            }
    client = Connect(config)
    sql = "SELECT * FROM t_mission "
    while 1:
        a = client.loadSql(sql)
        print(a)
        time.sleep(3)